#coding=utf-8
#Boa:Dialog:DlgLogin

import wx

from product import CDataStaff,CDataTip
from product import CDataLog as syslog
from config import *

def create(parent):
    return DlgLogin(parent)

[wxID_DLGLOGIN, wxID_DLGLOGINBTNCANCEL, wxID_DLGLOGINBTNLOGIN, 
 wxID_DLGLOGINCBUSER, wxID_DLGLOGINTCPSW, wxID_DLGLOGINTPSW, 
 wxID_DLGLOGINTTIP, wxID_DLGLOGINTUSER, 
] = [wx.NewId() for _init_ctrls in range(8)]

class DlgLogin(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGLOGIN, name=u'DlgLogin',
              parent=prnt, pos=wx.Point(601, 274), size=wx.Size(426, 342),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u767b\u5f55\u4fe1\u606f')
        self.SetClientSize(wx.Size(418, 308))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.Center(wx.BOTH)

        self.tUser = wx.StaticText(id=wxID_DLGLOGINTUSER,
              label=u'\u7528\u6237\u540d\uff1a', name=u'tUser', parent=self,
              pos=wx.Point(56, 136), size=wx.Size(72, 14), style=0)

        self.tPsw = wx.StaticText(id=wxID_DLGLOGINTPSW,
              label=u'\u5bc6  \u7801\uff1a', name=u'tPsw', parent=self,
              pos=wx.Point(56, 176), size=wx.Size(72, 14), style=0)

        self.tcPsw = wx.TextCtrl(id=wxID_DLGLOGINTCPSW, name=u'tcPsw',
              parent=self, pos=wx.Point(136, 168), size=wx.Size(208, 24),
              style=wx.TE_PASSWORD, value=u'')

        self.btnLogin = wx.Button(id=wxID_DLGLOGINBTNLOGIN,
              label=u'\u767b\u5f55', name=u'btnLogin', parent=self,
              pos=wx.Point(112, 232), size=wx.Size(75, 24), style=0)
        self.btnLogin.SetDefault()
        self.btnLogin.Bind(wx.EVT_BUTTON, self.OnBtnLoginButton,
              id=wxID_DLGLOGINBTNLOGIN)

        self.btnCancel = wx.Button(id=wxID_DLGLOGINBTNCANCEL,
              label=u'\u9000\u51fa', name=u'btnCancel', parent=self,
              pos=wx.Point(232, 232), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGLOGINBTNCANCEL)

        self.tTip = wx.StaticText(id=wxID_DLGLOGINTTIP,
              label=u'\u6bcf\u65e5\u4e00\u53e5', name=u'tTip', parent=self,
              pos=wx.Point(40, 16), size=wx.Size(336, 88), style=0)
        self.tTip.SetForegroundColour(wx.Colour(0, 0, 255))
        self.tTip.SetBackgroundColour(wx.Colour(222, 240, 213))

        self.cbUser = wx.ComboBox(choices=[], id=wxID_DLGLOGINCBUSER,
              name=u'cbUser', parent=self, pos=wx.Point(136, 128),
              size=wx.Size(208, 24), style=0, value=u'')
        self.cbUser.SetLabel(u'')

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.myInit()
        
    def myInit(self):
        szScreen = Cfg.getScreenSize()
        szSelf = self.GetSize()
        self.Move(((szScreen[0]-szSelf[0])/2, (szScreen[1]-szSelf[1])/2))
        
        #self.tTip.SetLabel(u'工具的存在是为了辅助我们，而不是约束我们！')
        #self.tTip.SetSize(self.tTip.GetBestSize())
        self.tTip.SetLabel(CDataTip.getRandomTip())
        self.tTip.SetSize(wx.Size(336, 88))
    
        self.loadUser()
        
    def loadUser(self):
        '''从文件读入先从gbk转换为unicode, 保存时则相反'''
        import ConfigParser,os
        cf=ConfigParser.ConfigParser()
        #fp = codecs.open('config.ini', "r", 'utf-8-sig')
        cf.readfp(open('config.ini', "r"))
    
        #self.cbUser.SetValue(unicode(cf.get("login","lastUser")))
        
        self.userList = cf.get("login","userList").decode('gbk').split(',')
        for user in self.userList:
            print (user, type(user))
            self.cbUser.Append(user)
    
        self.cbUser.SetValue(cf.get("login","lastUser").decode('gbk'))
        
    def saveCurUser(self, user):
        import ConfigParser,os
        
        cf=ConfigParser.ConfigParser()
        
        cf.readfp(open('config.ini', "r+"))    
        
        if user not in self.userList:
            self.userList.append(user)
        
        # unicode 没有join()
        newList = ''
        for item in self.userList:
            newList += item.encode('gbk') + ','
        newList = newList[:-1]
        
        user = user.encode('gbk')
        #print (type(newList), newList)
        #print (type(user), user)
        cf.set("login","userList", newList)
        cf.set("login","lastUser", user)
        cf.write(open('config.ini', "r+"))
        #cf.write(codecs.open('config.ini', "r+", 'utf-8-sig'))
            
    def OnBtnLoginButton(self, event):
        user = self.cbUser.GetValue()
        psw = self.tcPsw.GetValue()
        
        staff = CDataStaff.login(user, psw)
        if (staff is None):
            dlg = wx.MessageDialog(self, u'用户名或密码不正确，请重新输入', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        self.saveCurUser(user)
        
        Cfg.setCurUser(staff)
        
        syslog.info(u'系统启动')
        syslog.info(u'<%s>登录系统', staff.getValue(staff.COL_NAME))
        syslog.debug(u'<%s>登录系统', (staff.getValue(staff.COL_NAME)))
        syslog.debug(u'<%s>登录系统，用户名<%s>', (staff.getValue(staff.COL_NAME),staff.getValue(staff.COL_USER)))
        syslog.debug(u'<%s>登录系统，用户名<%s>', staff.getValue(staff.COL_NAME),staff.getValue(staff.COL_USER))
        
        self.EndModal(wx.ID_OK)
        
    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)
